<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close" append-to-body>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
			<el-row>
				<el-col :span="12">
					<el-form-item label="产品小类" prop="catalog3" :rules="rules.validateRequire">
						<el-select v-model="model.catalog3" filterable clearable :disabled="publishDisabled">
							<el-option v-for="item in codeModel.catalog3List" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="floorPricePermission">
					<el-form-item label="红线价格（元）" prop="floorprice" :rules="baseRules.checkPositiveFloatRequire">
						<el-input v-model="model.floorprice"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="备注" prop="description">
						<el-input v-model="model.description" type="textarea" :rows="4" :disabled="publishDisabled"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="排序码" prop="sortcode">
						<el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12"  v-if="!isAdd">
					<el-form-item label="启用" prop="enabled">
						<el-radio-group v-model="model.enabled" :disabled="disableEditSubmit || model.publishstate === '0'">
							<el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
			<el-button type="primary" @click="openChangeHistoryDialog('historyList')" v-if="model.id && model.publishstate === '1' && floorPricePermission">修改历史</el-button>
			<el-button type="primary" @click="publishProduct" :loading="saveLoading" v-if="disableEditSubmit === false && model.id && model.publishstate === '0'">发布</el-button>
			<el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">{{isAdd || model.publishstate === '0' ? '暂存' : '确认'}}</el-button>
    </div>
		<change-log-history ref="historyList" sourceType="MmsProductFloorPriceInfo" topSourceType="MmsProductFloorPriceInfo" :topSourceId="model.id + ''"></change-log-history>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import changeLogHistory from "./changeLogHistory"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { checkPositiveFloat } from '@/utils/numberValidate'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { changeLogHistory },
  data() {
    return {
      moduleTitle: '产品红线价格信息表',
      urlPrefix: '/mms/productFloorPriceInfo/',
      url: {
        // 额外的url
				publishUrl: '/mms/productFloorPriceInfo/publish'
      },
			baseRules: {
					validateRequire: [
							// 必填
							{ required: 'true', message: '请输入内容', trigger: 'change' }
					],
					checkPositiveFloatRequire: [
              { validator: checkPositiveFloat }
          ]
			}
    }
  },
	props: {
			productInfo: {
					type: Object,
					default: function() {
							return {}
					}
			},
      floorPricePermission: {
					type: Boolean,
					default: false
			}
	},
	computed: {
			publishDisabled() {
					if (this.model.publishstate && this.model.publishstate === '1') {
							return true
					}
					return false
			}
	},
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
		assignDefaultValue() {
        this.$set(this.model, 'productid', this.productInfo.id)
        this.$set(this.model, 'productname', this.productInfo.name)
        this.$set(this.model, 'productline', this.productInfo.productline)
        this.$set(this.model, 'catalog1', this.productInfo.catalog1)
        this.$set(this.model, 'catalog2', this.productInfo.catalog2)
		},
		publishProduct() {
				this.saveLoading = true
				getAction(this.url.publishUrl, { id: this.model.id }).then((res) => {
						if (res.success) {
								this.$tap.message('发布成功', 'success')
								this.visible = false
								this.$emit('refresh')
						} else {
								this.$tap.alert(res.message, 'warning')
						}
						this.saveLoading = false
				}).catch((err) => {
						this.saveLoading = false
						this.$tap.alert(err.message, 'warning')
				})
		},
		doAdd() {
				this.saveLoading = true
				postAction(this.url.add + '?billuid=' + this.uid, this.model)
						.then((res) => {
								if (res.success) {
										this.$tap.message('添加成功', 'success')
										this.model = res.result
										this.isAdd = false
										this.$emit('refresh')
								} else {
										this.$tap.alert(res.message, 'warning')
								}
								this.afterSubmit()
								this.saveLoading = false
						})
						.catch((err) => {
								this.saveLoading = false
						})
		},
		doEdit() {
				this.saveLoading = true
				putAction(this.url.edit, this.model)
						.then((res) => {
								if (res.success) {
										this.$tap.message(res.message, 'success')
										this.$emit('refresh')
								} else {
										this.$tap.alert(res.message, 'warning')
								}
								this.afterSubmit()
								this.saveLoading = false
						})
						.catch((err) => {
								this.saveLoading = false
						})
		},
		openChangeHistoryDialog(refName) {
        this.$refs[refName].visible = true
		}
  }
}
</script>

